using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class pages_FaclicilyBooking : System.Web.UI.Page
{
    FacilityBooking fb = new FacilityBooking();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {            
            int id = int.Parse(Request["id"]);
            FacilityDAO daoFac=new FacilityDAO();
            Facility fac = daoFac.GetFacility(id);
            fb.Facility = fac;
            lblFacility.Text = fac.Name;
            lblPrice.Text = Convert.ToString(fac.Price-fac.Discount);
            Session["f_booking"] = fac;
            User user=Session["user"] as User;
            if (user != null)
            {
                if (user.PersonInfor.Id <= 0) return;
                PersonDAO daoPerson = new PersonDAO();
                user.PersonInfor = daoPerson.GetPerson(user.PersonInfor.Id);
                Session["user"] = user;
            }
            //load data from variable 'user' into form
            if(IsRegisteredUser(user))uctPersonInfo.LoadData();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void lnkAccept_Click(object sender, EventArgs e)
    {
        Validate();
        if (!IsValid) return;
        GetPersonInfo();
        User user = Session["user"] as User;
        if (user == null) //if error occur, system can get customer information
        {
            lblMessage.Text = "Sorry. Error occured when tryinging to create your booking in our system.<br>Please try later or contact us";
            return;

        }
        if (IsRegisteredUser(user)) AddBookingForRegisteredUser(user);
        else
        {
            user.PersonInfor.ModifiedDate = DateTime.Now;
            AddBookingForUnregisterUser(user.PersonInfor);

        }
        
    }

    protected void lnkChange_Click(object sender, EventArgs e)
    {
        GetPersonInfo();
    }
    /// <summary>
    /// Keep personal information in form
    /// </summary>
    public void GetPersonInfo()
    {      
        User user = Session["user"] as User;
        if (user == null) user = new User();
        user.PersonInfor.FirstName = uctPersonInfo.FirstName;
        user.PersonInfor.LastName = uctPersonInfo.LastName;
        user.PersonInfor.DateOfBirth = uctPersonInfo.DateOfBirth;
        user.PersonInfor.Email = uctPersonInfo.Email;
        user.PersonInfor.Gender = uctPersonInfo.Gender;
        user.PersonInfor.Phone = uctPersonInfo.Phone;
        user.PersonInfor.Country = uctPersonInfo.Country;
        user.PersonInfor.State = uctPersonInfo.State;
        user.PersonInfor.City = uctPersonInfo.City;
        user.PersonInfor.Address = uctPersonInfo.Address;
        Session["user"] = user;
    }
    protected void lnkChange_Click1(object sender, EventArgs e)
    {
        GetPersonInfo();
    }
    /// <summary>
    /// Check if a user is registered user
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public bool IsRegisteredUser(User user)
    {
        return user.Id > 0;
    }

    public void AddBookingForRegisteredUser(User user)
    {
        BookingDAO daoBooking = new BookingDAO();
        FacilityBookingDAO daoFacBooking = new FacilityBookingDAO();

        //user = Session["user"] as FacilityBooking;
        if (user == null)
        {
            lblMessage.Text = "Error";
            return;
        }
        fb.CustomerID = user.PersonInfor.Id;
        fb.RentTime = DateTime.Parse(txtRentDate.Text);
        fb.Quantity = int.Parse(txtQuantity.Text);
        fb.Price = fb.Facility.Price - fb.Facility.Discount;
        fb.Type = 2;
        fb.ModifiedDate = DateTime.Now;
        fb.ModifiedBy = user.Id;
        fb.Note = txtNote.Text;
        fb.During = int.Parse(txtDuring.Text);
        if (daoBooking.Insert(fb) > 0)
        {
            if (daoFacBooking.Insert(fb) > 0)
            {
                Session["f_booking"] = fb;
                Response.Redirect("CheckoutFacilityBooking.aspx");
            }
            else lblMessage.Text = "Sorry! Some errors has ocurred in our system.<br/> Please try later or contact us now";
        }
        else lblMessage.Text = "Sorry! Some errors has ocurred in our system.<br/> Please try later or contact us now";
    
    }

    public void AddBookingForUnregisterUser(Person perInfo)
    {
        PersonDAO daoPerson = new PersonDAO();
        BookingDAO daoBooking = new BookingDAO();
        FacilityBookingDAO daoFacBooking = new FacilityBookingDAO();


        fb.RentTime = DateTime.Parse(txtRentDate.Text);
        fb.Quantity = int.Parse(txtQuantity.Text);
        fb.Note = txtNote.Text;
        fb.Type = 2;//type=2 means facility booking
        fb.During = int.Parse(txtDuring.Text);
        if (daoPerson.Insert(perInfo) <= 0)
        {
            lblMessage.Text = "Error";
            return;
        }
        fb.ModifiedDate = DateTime.Now;
        fb.CustomerID = perInfo.Id;      
        if (daoBooking.Insert(fb) <= 0)
        {
            lblMessage.Text = "Error when create new booking";
            return;
        }
        if (daoFacBooking.Insert(fb) <= 0)
        {
            lblMessage.Text = "Error when trying to create package booking";
            return;
        }
        Session["f_booking"] = fb;
        Response.Redirect("CheckoutFacilityBooking.aspx");
    }
  
}
